package com.ymatou.promotion.api.testcase;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.promotion.api.parameter.CouponBatchBean;
import com.ymatou.promotion.api.parameter.CouponValueInfoBean;
import com.ymatou.promotion.api.parameter.CreateSellerCouponBean;
import com.ymatou.promotion.api.parameter.ReceiveBatchCoupon;
import com.ymatou.promotion.api.parameter.ReceiveCouponInfoBatchBean;
import com.ymatou.promotion.api.parameter.ReceiveCouponInfoBatchData;
import com.ymatou.promotion.api.service.CreateSellerCouponCall;
import com.ymatou.promotion.api.service.ReceiveCouponInfoBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponScenario;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.model.YmtCouponValue;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponScenarioWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponValueWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 多用户批量领取优惠券接口
 *
 * @author chenjiazhu
 */
@TestSuite
public class Ts_ReceiveCouponInfoBatch {
    private static ReceiveCouponInfoBatchBean receivecouponinfobatchBean;
    private static ReceiveCouponInfoBatchCall receivecouponinfobatchCall;

    private static CreateSellerCouponBean createsellercouponBean;
    private static CreateSellerCouponCall createsellercouponCall;

    private static CouponBatchBean couponBatchBean;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("多用户批量领取优惠券接口");
    }

    @Before
    public void caseUp() {
        createsellercouponBean = new CreateSellerCouponBean();
        createsellercouponCall = new CreateSellerCouponCall();

        receivecouponinfobatchBean = new ReceiveCouponInfoBatchBean();
        receivecouponinfobatchCall = new ReceiveCouponInfoBatchCall();

        couponBatchBean = new CouponBatchBean();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(receivecouponinfobatchCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_001() {
        Logger.start(true, "多用户批量领取全场优惠券");
        try {
            setReceiveCouponInfoBatchBean(1);

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_002() {
        Logger.start(true, "多用户批量领取商品优惠券");
        try {

            setReceiveCouponInfoBatchBean(2);

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_003() {
        Logger.start(true, "多用户批量领取全场优惠券-洋码头PC端");
        try {

            setReceiveCouponInfoBatchBean(1, "1");

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_004() {
        Logger.start(true, "多用户批量领取全场优惠券-洋码头WAP端");
        try {

            setReceiveCouponInfoBatchBean(1, "2");

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_005() {
        Logger.start(true, "多用户批量领取全场优惠券-洋码头APP");
        try {

            setReceiveCouponInfoBatchBean(1, "3");

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_006() {
        Logger.start(true, "多用户批量领取全场优惠券-神器APP");
        try {

            setReceiveCouponInfoBatchBean(1, "4");

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_007() {
        Logger.start(true, "多用户批量领取全场优惠券-神器WAP");
        try {

            setReceiveCouponInfoBatchBean(1, "5");

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_008() {
        Logger.start(true, "多用户批量领取商品优惠券-洋码头PC端");
        try {

            setReceiveCouponInfoBatchBean(2, "1");

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_009() {
        Logger.start(true, "多用户批量领取商品优惠券-洋码头WAP端");
        try {

            setReceiveCouponInfoBatchBean(2, "2");

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_010() {
        Logger.start(true, "多用户批量领取商品优惠券-洋码头APP");
        try {

            setReceiveCouponInfoBatchBean(2, "3");

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_011() {
        Logger.start(true, "多用户批量领取商品优惠券-神器APP");
        try {

            setReceiveCouponInfoBatchBean(2, "4");

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_012() {
        Logger.start(true, "多用户批量领取商品优惠券-神器WAP");
        try {

            setReceiveCouponInfoBatchBean(2, "5");

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_013() {
        Logger.start(false, "验证BuyerUserIds空时报错");
        try {

            setReceiveCouponInfoBatchBean(1);
            receivecouponinfobatchBean.setBuyerUserIds(null);

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            Logger.verifyEquals(ECode.ERROR500, receivecouponinfobatchCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, receivecouponinfobatchCall.getBCode(), "验证返回Bcode");

            Logger.verifyEquals(ECode.NullException_STRING, receivecouponinfobatchCall.getMsg(), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_014() {
        Logger.start(false, "验证BuyerUserIds不存在时报错");
        try {

            setReceiveCouponInfoBatchBean(1);

            List<Integer> users = new ArrayList<Integer>();
            users.add(0);
            users.add(99999);
            receivecouponinfobatchBean.setBuyerUserIds(users);

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, receivecouponinfobatchCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, receivecouponinfobatchCall.getBCode(), "验证返回Bcode");

            Logger.verifyEquals(ECode.COUPON_RECEIVECOUPONINFOBATCH_Fail_STRING01,
                    receivecouponinfobatchCall.getMessageText(), "验证返回MessageText");

            ReceiveCouponInfoBatchData receiveBatchCoupons = receivecouponinfobatchCall.getReceiveCouponInfoBatchData();

            for (ReceiveBatchCoupon receiveBatchCoupon : receiveBatchCoupons.getReceiveBatchCoupons()) {
                Logger.verifyEquals(ECode.COUPON_User_Not_Exist_STRING, receiveBatchCoupon.getMessage(),
                        "验证返回MessageText");
            }

            ReceiveCouponInfoBatchData receiveCouponInfoBatchData =
                    receivecouponinfobatchCall.getReceiveCouponInfoBatchData();

            Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveUserCount(), "验证返回ReceiveUserCount");
            Logger.verifyEquals(0, receiveCouponInfoBatchData.getReceiveSuccessUserCount(),
                    "验证返回ReceiveSuccessUserCount");
            Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveFailureUserCount(),
                    "验证返回ReceiveFailureUserCount");


        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_015() {
        Logger.start(false, "验证BatchCode空时报错");
        try {

            setReceiveCouponInfoBatchBean(1);

            receivecouponinfobatchBean.setBatchCode(null);

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, receivecouponinfobatchCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, receivecouponinfobatchCall.getBCode(), "验证返回Bcode");

            Logger.verifyEquals(ECode.COUPON_RECEIVECOUPONINFOBATCH_Fail_STRING03,
                    receivecouponinfobatchCall.getMessageText(), "验证返回MessageText");

            ReceiveCouponInfoBatchData receiveCouponInfoBatchData =
                    receivecouponinfobatchCall.getReceiveCouponInfoBatchData();

            Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveUserCount(), "验证返回ReceiveUserCount");
            Logger.verifyEquals(0, receiveCouponInfoBatchData.getReceiveSuccessUserCount(),
                    "验证返回ReceiveSuccessUserCount");
            Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveFailureUserCount(),
                    "验证返回ReceiveFailureUserCount");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_016() {
        Logger.start(false, "验证BatchCode不存在时报错");
        try {

            setReceiveCouponInfoBatchBean(1);

            receivecouponinfobatchBean.setBatchCode("123");

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, receivecouponinfobatchCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, receivecouponinfobatchCall.getBCode(), "验证返回Bcode");

            Logger.verifyEquals(ECode.COUPON_RECEIVECOUPONINFOBATCH_Fail_STRING03,
                    receivecouponinfobatchCall.getMessageText(), "验证返回MessageText");

            ReceiveCouponInfoBatchData receiveCouponInfoBatchData =
                    receivecouponinfobatchCall.getReceiveCouponInfoBatchData();

            Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveUserCount(), "验证返回ReceiveUserCount");
            Logger.verifyEquals(0, receiveCouponInfoBatchData.getReceiveSuccessUserCount(),
                    "验证返回ReceiveSuccessUserCount");
            Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveFailureUserCount(),
                    "验证返回ReceiveFailureUserCount");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_017() {
        Logger.start(false, "验证BatchCode领完后再领失败");
        try {
            setReceiveCouponInfoBatchBean(1);

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            // 再次领失败
            receivecouponinfobatchCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, receivecouponinfobatchCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, receivecouponinfobatchCall.getBCode(), "验证返回Bcode");

            Logger.verifyEquals(ECode.COUPON_RECEIVECOUPONINFOBATCH_Fail_STRING01,
                    receivecouponinfobatchCall.getMessageText(), "验证返回MessageText");

            ReceiveCouponInfoBatchData receiveCouponInfoBatchData =
                    receivecouponinfobatchCall.getReceiveCouponInfoBatchData();

            Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveUserCount(), "验证返回ReceiveUserCount");
            Logger.verifyEquals(0, receiveCouponInfoBatchData.getReceiveSuccessUserCount(),
                    "验证返回ReceiveSuccessUserCount");
            Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveFailureUserCount(),
                    "验证返回ReceiveFailureUserCount");


        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_018() {
        Logger.start(false, "验证ValidStart格式不正确时报错");
        try {

            setReceiveCouponInfoBatchBean(1);

            receivecouponinfobatchBean.setValidStart("20160101");

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, receivecouponinfobatchCall.getCode(), "验证返回code-有点不理解，居然过来");
            Logger.verifyEquals(ECode.BCODE0, receivecouponinfobatchCall.getBCode(), "验证返回Bcode");

            Logger.verifyEquals(ECode.COUPON_RECEIVECOUPONINFOBATCH_SUCCESS_STRING,
                    receivecouponinfobatchCall.getMessageText(), "验证返回MessageText");

            ReceiveCouponInfoBatchData receiveCouponInfoBatchData =
                    receivecouponinfobatchCall.getReceiveCouponInfoBatchData();

            Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveUserCount(), "验证返回ReceiveUserCount");
            Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveSuccessUserCount(),
                    "验证返回ReceiveSuccessUserCount");
            Logger.verifyEquals(0, receiveCouponInfoBatchData.getReceiveFailureUserCount(),
                    "验证返回ReceiveFailureUserCount");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_019() {
        Logger.start(false, "验证ValidEnd格式不正确时报错");
        try {

            setReceiveCouponInfoBatchBean(1);

            receivecouponinfobatchBean.setValidEnd("20160101");

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, receivecouponinfobatchCall.getCode(), "验证返回code-有点不理解，居然过来");
            Logger.verifyEquals(ECode.BCODE0, receivecouponinfobatchCall.getBCode(), "验证返回Bcode");

            Logger.verifyEquals(ECode.COUPON_RECEIVECOUPONINFOBATCH_SUCCESS_STRING,
                    receivecouponinfobatchCall.getMessageText(), "验证返回MessageText");

            ReceiveCouponInfoBatchData receiveCouponInfoBatchData =
                    receivecouponinfobatchCall.getReceiveCouponInfoBatchData();

            Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveUserCount(), "验证返回ReceiveUserCount");
            Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveSuccessUserCount(),
                    "验证返回ReceiveSuccessUserCount");
            Logger.verifyEquals(0, receiveCouponInfoBatchData.getReceiveFailureUserCount(),
                    "验证返回ReceiveFailureUserCount");


        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_020() {
        Logger.start(false, "验证ValidStart,ValidEnd不填时，取优惠券的开始结束时间");
        try {

            setReceiveCouponInfoBatchBean(1);

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_021() {
        Logger.start(false, "验证ValidStart,ValidEnd区间大于优惠券的开始结束时间时取值");
        try {

            setReceiveCouponInfoBatchBean(1);

            Date dNow = new Date(); // 当前时间
            Date dBefore = new Date();
            Date dAfter = new Date();

            Calendar calendar1 = Calendar.getInstance(); // 得到日历
            calendar1.setTime(dNow);// 把当前时间赋给日历
            calendar1.add(Calendar.DAY_OF_MONTH, -2); // 设置为前一天
            dBefore = calendar1.getTime(); // 得到前一天的时间

            Calendar calendar2 = Calendar.getInstance(); // 得到日历
            calendar2.setTime(dNow);// 把当前时间赋给日历
            calendar2.add(Calendar.DAY_OF_MONTH, 11); // 设置为后十天
            dAfter = calendar2.getTime(); // 得到前一天的时间

            SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");

            receivecouponinfobatchBean.setValidStart(dateFormater.format(dBefore));
            receivecouponinfobatchBean.setValidEnd(dateFormater.format(dAfter));

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    @Ignore
    public void Tc_ReceiveCouponInfoBatch_022() {
        Logger.start(true, "验证ValidStart,ValidEnd落地正确_需确认");
        try {

            setReceiveCouponInfoBatchBean(1);

            Date dNow = new Date(); // 当前时间
            Date dBefore = new Date();
            Date dAfter = new Date();

            Calendar calendar2 = Calendar.getInstance(); // 得到日历
            calendar2.setTime(dNow);// 把当前时间赋给日历
            calendar2.add(Calendar.DAY_OF_MONTH, 5); // 设置为后十天
            dAfter = calendar2.getTime(); // 得到前一天的时间

            SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");

            receivecouponinfobatchBean.setValidStart(dateFormater.format(dNow));
            receivecouponinfobatchBean.setValidEnd(dateFormater.format(dAfter));

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, receivecouponinfobatchCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, receivecouponinfobatchCall.getBCode(), "验证返回Bcode");

            ReceiveCouponInfoBatchData receiveCouponInfoBatchData =
                    receivecouponinfobatchCall.getReceiveCouponInfoBatchData();
            Logger.verifyEquals(ECode.COUPON_RECEIVECOUPONINFOBATCH_SUCCESS_STRING,
                    receiveCouponInfoBatchData.getMessageText(), "验证返回MessageText");

            Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveUserCount(), "验证返回ReceiveUserCount");
            Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveSuccessUserCount(),
                    "验证返回ReceiveSuccessUserCount");
            Logger.verifyEquals(0, receiveCouponInfoBatchData.getReceiveFailureUserCount(),
                    "验证返回ReceiveFailureUserCount");

            List<ReceiveBatchCoupon> receiveBatchCoupons = receiveCouponInfoBatchData.getReceiveBatchCoupons();

            for (ReceiveBatchCoupon receiveBatchCoupon : receiveBatchCoupons) {
                String couponCode = receiveBatchCoupon.getCouponCode();

                YmtCoupon coupon = new YmtCouponWapper().selectBysCouponCode(couponCode).get(0);
                YmtCouponPrivateUserBound couponPrivateUserBound =
                        new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(couponCode).get(0);
                YmtCouponBatch couponBatch = new YmtCouponBatchWapper().selectByPrimaryKey(coupon.getiBatchId());
                YmtCouponValue couponValue =
                        new YmtCouponValueWapper().selectByiCouponSettingId(couponBatch.getiCouponSettingId()).get(0);
                YmtCouponSetting couponSetting =
                        new YmtCouponSettingWapper().selectByiCouponSettingId(couponBatch.getiCouponSettingId()).get(0);
                YmtCouponScenario couponScenario =
                        new YmtCouponScenarioWapper().selectByiCouponScenarioId(couponSetting.getiScenarioId()).get(0);

                // 验证数据库中 UserId
                Logger.verifyEquals(receiveBatchCoupon.getUserId(), couponPrivateUserBound.getiUserId(),
                        "验证数据库表Ymt_CouponPrivateUserBound中iUserId");

                // 验证数据库BatchId
                Logger.verifyEquals(couponPrivateUserBound.getiBatchId(), couponBatch.getiBatchId(),
                        "验证数据库表Ymt_CouponBatch与表Ymt_CouponPrivateUserBound中iBatchId");

                // 验证数据库ReceiveCount
                Logger.verifyEquals(2, couponSetting.getiReceiveCount(), "验证数据库表Ymt_CouponSetting中iReceiveCount");

                // 验证数据库ValidStart
                Logger.verifyEquals(getNewData(dNow), coupon.getdValidStart(),
                        "验证数据库表Ymt_Coupon中ValidStart,数据落的还是setting的ValidStart,需跟开发确认下");

                // 验证数据库ValidEnd
                Logger.verifyEquals(getNewData(dAfter), coupon.getdValidEnd(),
                        "验证数据库表Ymt_Coupon中ValidEnd,数据落的还是setting的ValidStart,需跟开发确认下");

                // 验证数据库CouponUseType
                Logger.verifyEquals(couponBatchBean.getCouponValues().get(0).getCouponUseType(),
                        couponSetting.getiCouponUseType(), "验证数据库表Ymt_Coupon中ValidEnd");

                if (couponBatchBean.getUsePlatforms() != null && !couponBatchBean.getUsePlatforms().isEmpty())
                {
                    Logger.verifyEquals(couponBatchBean.getUsePlatforms(), couponScenario.getSuseplatforms(),
                            "数据库中Suseplatforms不正确");
                }
            }

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_ReceiveCouponInfoBatch_023() {
        Logger.start(true, "验证优惠券数只有一张时两个用户领取，只能一个用户领取成功");
        try {
            setReceiveCouponInfoBatchBean(2, "5", 1, 1);

            receivecouponinfobatchCall.setData(receivecouponinfobatchBean);
            receivecouponinfobatchCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, receivecouponinfobatchCall.getCode(), "验证返回code-有点不理解，居然过来");
            Logger.verifyEquals(ECode.BCODE0, receivecouponinfobatchCall.getBCode(), "验证返回Bcode");

            Logger.verifyEquals(ECode.COUPON_RECEIVECOUPONINFOBATCH_Fail_STRING02,
                    receivecouponinfobatchCall.getMessageText(), "验证返回MessageText");

            ReceiveCouponInfoBatchData receiveCouponInfoBatchData =
                    receivecouponinfobatchCall.getReceiveCouponInfoBatchData();

            Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveUserCount(), "验证返回ReceiveUserCount");
            Logger.verifyEquals(1, receiveCouponInfoBatchData.getReceiveSuccessUserCount(),
                    "验证返回ReceiveSuccessUserCount");
            Logger.verifyEquals(1, receiveCouponInfoBatchData.getReceiveFailureUserCount(),
                    "验证返回ReceiveFailureUserCount");

            Logger.verifyEquals(ECode.COUPON_RECEIVECOUPONINFOBATCH_01, receiveCouponInfoBatchData
                    .getReceiveBatchCoupons().get(0).getMessage(), "验证返回ReceiveFailureUserCount");
            Logger.verifyEquals(ECode.COUPON_RECEIVECOUPONINFOBATCH_02, receiveCouponInfoBatchData
                    .getReceiveBatchCoupons().get(1).getMessage(), "验证返回ReceiveFailureUserCount");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    /**
     * 设置默认创建优惠券的信息
     * 
     * @return
     */
    public CouponBatchBean setCouponBatchBean(int scenarioType, int maxSendNum, int userMaxReceiveNum)
    {
        couponBatchBean.setSellerId(Integer.valueOf(EnvSetup.getData("SellerId")));
        couponBatchBean.setCouponName("自动化创建优惠券名称" + TestDataManager.getRandomNum(5));
        couponBatchBean.setCouponDesc("自动化创建优惠券描述" + TestDataManager.getRandomNum(10));

        List<CouponValueInfoBean> couponValues = new ArrayList<CouponValueInfoBean>();

        CouponValueInfoBean couponValue = new CouponValueInfoBean();

        couponValue.setOrderValue(new BigDecimal(2));
        couponValue.setCouponValue(new BigDecimal(1));
        couponValue.setCouponUseType(1);

        couponValues.add(couponValue);

        couponBatchBean.setCouponValues(couponValues);
        couponBatchBean.setMaxSendNum(maxSendNum);
        couponBatchBean.setUserMaxReceiveNum(userMaxReceiveNum);

        Date dNow = new Date(); // 当前时间
        Date dBefore = new Date();
        Date dAfter = new Date();

        Calendar calendar1 = Calendar.getInstance(); // 得到日历
        calendar1.setTime(dNow);// 把当前时间赋给日历
        calendar1.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
        dBefore = calendar1.getTime(); // 得到前一天的时间

        Calendar calendar2 = Calendar.getInstance(); // 得到日历
        calendar2.setTime(dNow);// 把当前时间赋给日历
        calendar2.add(Calendar.DAY_OF_MONTH, 10); // 设置为后十天
        dAfter = calendar2.getTime(); // 得到前一天的时间

        couponBatchBean.setValidStart(dBefore);
        couponBatchBean.setValidEnd(dAfter);

        couponBatchBean.setScenarioType(scenarioType);

        return couponBatchBean;
    }

    /**
     * 设置默认创建优惠券的信息
     * 
     * @return
     */
    public CouponBatchBean setCouponBatchBean(int scenarioType, String usePlatforms, int maxSendNum,
            int userMaxReceiveNum)
    {
        CouponBatchBean couponBatchBean = setCouponBatchBean(scenarioType, maxSendNum, userMaxReceiveNum);

        couponBatchBean.setUsePlatforms(usePlatforms);

        return couponBatchBean;
    }

    // 根据传入bean创建优惠券
    public void createSellerCoupon(CouponBatchBean couponBatchBean) throws ClientProtocolException, IOException
    {
        createsellercouponBean.setCouponBatchBean(couponBatchBean);
        createsellercouponCall.setData(false, createsellercouponBean);
        createsellercouponCall.callService();
    }

    private void setReceiveCouponInfoBatchBean(int scenarioType, int maxSendNum, int userMaxReceiveNum)
            throws ClientProtocolException, IOException
    {
        setCouponBatchBean(scenarioType, maxSendNum, userMaxReceiveNum);
        createSellerCoupon(couponBatchBean);

        List<Integer> buyerUserIds = new ArrayList<Integer>();
        buyerUserIds.add(Integer.parseInt(EnvSetup.getData("BuyUserId")));
        buyerUserIds.add(Integer.parseInt(EnvSetup.getData("BuyerId")));

        receivecouponinfobatchBean.setBuyerUserIds(buyerUserIds);
        receivecouponinfobatchBean.setBatchCode(createsellercouponCall.getBatchCode());
    }

    private void setReceiveCouponInfoBatchBean(int scenarioType) throws ClientProtocolException, IOException
    {
        setReceiveCouponInfoBatchBean(scenarioType, 2, 1);
    }

    private void setReceiveCouponInfoBatchBean(int scenarioType, String usePlatforms, int maxSendNum,
            int userMaxReceiveNum) throws ClientProtocolException, IOException
    {
        CouponBatchBean couponBatchBean = setCouponBatchBean(scenarioType, usePlatforms, maxSendNum, userMaxReceiveNum);
        createSellerCoupon(couponBatchBean);

        List<Integer> buyerUserIds = new ArrayList<Integer>();
        buyerUserIds.add(Integer.parseInt(EnvSetup.getData("BuyUserId")));
        buyerUserIds.add(Integer.parseInt(EnvSetup.getData("BuyerId")));

        receivecouponinfobatchBean.setBuyerUserIds(buyerUserIds);
        receivecouponinfobatchBean.setBatchCode(createsellercouponCall.getBatchCode());
    }

    private void setReceiveCouponInfoBatchBean(int scenarioType, String usePlatforms) throws ClientProtocolException,
            IOException
    {
        setReceiveCouponInfoBatchBean(scenarioType, usePlatforms, 2, 1);
    }

    private void verifyReturn()
    {
        Logger.verifyEquals(ECode.SUCCESS, receivecouponinfobatchCall.getCode(), "验证返回code");
        Logger.verifyEquals(ECode.BCODE0, receivecouponinfobatchCall.getBCode(), "验证返回Bcode");

        ReceiveCouponInfoBatchData receiveCouponInfoBatchData =
                receivecouponinfobatchCall.getReceiveCouponInfoBatchData();
        Logger.verifyEquals(ECode.COUPON_RECEIVECOUPONINFOBATCH_SUCCESS_STRING,
                receiveCouponInfoBatchData.getMessageText(), "验证返回MessageText");

        Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveUserCount(), "验证返回ReceiveUserCount");
        Logger.verifyEquals(2, receiveCouponInfoBatchData.getReceiveSuccessUserCount(), "验证返回ReceiveSuccessUserCount");
        Logger.verifyEquals(0, receiveCouponInfoBatchData.getReceiveFailureUserCount(), "验证返回ReceiveFailureUserCount");

        List<ReceiveBatchCoupon> receiveBatchCoupons = receiveCouponInfoBatchData.getReceiveBatchCoupons();

        for (ReceiveBatchCoupon receiveBatchCoupon : receiveBatchCoupons) {
            String couponCode = receiveBatchCoupon.getCouponCode();

            YmtCoupon coupon = new YmtCouponWapper().selectBysCouponCode(couponCode).get(0);
            YmtCouponPrivateUserBound couponPrivateUserBound =
                    new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(couponCode).get(0);
            YmtCouponBatch couponBatch = new YmtCouponBatchWapper().selectByPrimaryKey(coupon.getiBatchId());
            YmtCouponValue couponValue =
                    new YmtCouponValueWapper().selectByiCouponSettingId(couponBatch.getiCouponSettingId()).get(0);
            YmtCouponSetting couponSetting =
                    new YmtCouponSettingWapper().selectByiCouponSettingId(couponBatch.getiCouponSettingId()).get(0);
            YmtCouponScenario couponScenario =
                    new YmtCouponScenarioWapper().selectByiCouponScenarioId(couponSetting.getiScenarioId()).get(0);

            // 验证数据库中 UserId
            Logger.verifyEquals(receiveBatchCoupon.getUserId(), couponPrivateUserBound.getiUserId(),
                    "验证数据库表Ymt_CouponPrivateUserBound中iUserId");

            // 验证数据库BatchId
            Logger.verifyEquals(couponPrivateUserBound.getiBatchId(), couponBatch.getiBatchId(),
                    "验证数据库表Ymt_CouponBatch与表Ymt_CouponPrivateUserBound中iBatchId");

            // 验证数据库ReceiveCount
            Logger.verifyEquals(2, couponSetting.getiReceiveCount(), "验证数据库表Ymt_CouponSetting中iReceiveCount");

            // 验证数据库ValidStart
            Logger.verifyEquals(getNewData(couponSetting.getdValidStart()), coupon.getdValidStart(),
                    "验证数据库表Ymt_Coupon中ValidStart");

            // 验证数据库ValidEnd
            Logger.verifyEquals(getNewData(couponSetting.getdValidEnd()), coupon.getdValidEnd(),
                    "验证数据库表Ymt_Coupon中ValidEnd");

            // 验证数据库CouponUseType
            Logger.verifyEquals(couponBatchBean.getCouponValues().get(0).getCouponUseType(),
                    couponSetting.getiCouponUseType(), "验证数据库表Ymt_Coupon中ValidEnd");

            if (couponBatchBean.getUsePlatforms() != null && !couponBatchBean.getUsePlatforms().isEmpty())
            {
                Logger.verifyEquals(couponBatchBean.getUsePlatforms(), couponScenario.getSuseplatforms(),
                        "数据库中Suseplatforms不正确");
            }

            // 验证返回IsSuccess

            // "ValidStart":"0001-01-01T00:00:00","ValidEnd":"0001-01-01T00:00:00","CouponUseType":0,"MinOrderValue":0.0,"CouponValue":0.0
            // 这些字段返回有问题
        }

    }

    private Date getNewData(Date date)
    {
        return new Date(date.getYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes());
    }
}
